﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL.DataOperate
{
    public class ShoppingCarDAL : DAL.App_Code.DBBase
    {
        public ShoppingCarDAL() :
            base() { }

        public DataSet SelectByUserId(int UserId)
        {
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("select * from vi_ShoppingCar ");
            sqlStr.Append("where User_Idint = ");
            sqlStr.Append("@User_Idint");

            SqlParameter[] pa = {
                new SqlParameter("@User_Idint",UserId)
                                };

            return ExecuteString(sqlStr.ToString(), pa, "tbShoppingCar");
        }
        public DataSet SelectByBookUserId(int BookId,int UserId)
        {
            StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("select * from vi_ShoppingCar ");
            sqlStr.Append("where User_Idint = ");
            sqlStr.Append("@User_Idint ");
            sqlStr.Append("and Book_Idint = ");
            sqlStr.Append("@Book_Idint ");

            SqlParameter[] pa = {
                new SqlParameter("@User_Idint",UserId),
                new SqlParameter("@Book_Idint",BookId)
                                };

            return ExecuteString(sqlStr.ToString(), pa, "tbShoppingCar");
        }
        public bool Insert(Model.ShoppingCar car)
        {
            StringBuilder sqlStr = new StringBuilder();
            DataSet ds = SelectByBookUserId(car.Book_Idint, car.User_Idint);
            int affectedRow = 0;

            if ((ds == null) ||
               (ds.Tables.Count == 0) ||
               (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
                sqlStr.Append("insert into vi_ShoppingCar(Book_Idint,User_Idint,Car_Quantityint) ");
                sqlStr.Append("values(@Book_Idint,@User_Idint,@Car_Quantityint)");

                SqlParameter[] pa = {
                new SqlParameter("@Book_Idint",car.Book_Idint),
                new SqlParameter("@User_Idint",car.User_Idint),
                new SqlParameter("@Car_Quantityint",car.Car_Quantityint)
                                };

                ExecuteString(sqlStr.ToString(), pa, out affectedRow);
                return affectedRow > 0 ? true : false;
            }
            else  // DO Noting
            { return true; }
        }
        public bool Delete(int CarId)
        {
            StringBuilder sqlStr = new StringBuilder();
            int affectedRow = 0;
            sqlStr.Append("delete from vi_ShoppingCar ");
            sqlStr.Append("where Car_Idint = ");
            sqlStr.Append("@Car_Idint");

            SqlParameter[] pa = {
                new SqlParameter("@Car_Idint",CarId)
                                };

            ExecuteString(sqlStr.ToString(), pa, out affectedRow);
            return affectedRow > 0 ? true : false;
        }
        public bool Delete(int BookId, String UserName)
        {
            StringBuilder sqlStr = new StringBuilder();
            //------------ 查询用户编号
            DAL.DataOperate.UserDAL userDAL = new UserDAL();
            DataSet ds = new DataSet();
            int userId = 0;
            ds = userDAL.SelectByName(UserName);
            if ((ds == null) ||
              (ds.Tables.Count == 0) ||
              (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            { return false; }
            else
            {
                userId = Int32.Parse(ds.Tables[0].Rows[0]["User_Idint"].ToString());

                int affectedRow = 0;
                sqlStr.Append("delete from vi_ShoppingCar ");
                sqlStr.Append("where Book_Idint = ");
                sqlStr.Append("@Book_Idint ");
                sqlStr.Append("and User_Idint = ");
                sqlStr.Append("@User_Idint ");

                SqlParameter[] pa = {
                new SqlParameter("@Book_Idint",BookId),
                new SqlParameter("@User_Idint",userId)
                                };

                ExecuteString(sqlStr.ToString(), pa, out affectedRow);
                return affectedRow > 0 ? true : false;
            }
        }
    }
}
